package com.hyt.it.ogt.ks.service.Event;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import javax.annotation.Resource;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationListener;

import com.hyt.it.ogt.ks.config.ConfigManager;
import com.hyt.it.ogt.ks.model.dto.ReportAndSendScoreDayiDTO;
import com.hyt.it.ogt.ks.service.IThirdInterfaceService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author wuyingge
 * @ClassName ReportAndSendSocreDayiListener.java
 * @Description 向大易推送考试结果
 * @createTime 2021年10月27日 16:40:00
 */
//@Component
@Slf4j
public class ReportAndSendSocreDayiListener implements ApplicationListener<ReportAndSendSocreDayiEvent>, DisposableBean {

    private final List<ReportAndSendScoreDayiDTO> jobs = new ArrayList<>();
    private final AtomicInteger jobsSize = new AtomicInteger(0);
    private final Lock lock = new ReentrantLock(true);

    @Resource
    private IThirdInterfaceService iThirdInterfaceService;
    @Resource
    private ConfigManager configManager;

    @Override
    //@Async
    public void onApplicationEvent(ReportAndSendSocreDayiEvent event) {
        Integer timeConsumer = event.getTimeConsumer();
        if(Objects.nonNull(timeConsumer)){
            timeToConsumer();
            return;
        }
        ReportAndSendScoreDayiDTO job = event.getJob();
        if(Objects.isNull(job) || Objects.isNull(job.getCandidateId()) || Objects.isNull(job.getExamId())){
            log.error("收到向大易推送考试结果事件，数据为空:{}",event);
        }
        log.debug("收到向大易推送考试结果事件通，收到数据为:{}",event);
        putJobToList(job);
    }

    private void putJobToList(ReportAndSendScoreDayiDTO job){
        try {
            lock.lock();
            jobs.add(job);
            int num = jobsSize.addAndGet(1);
            if(configManager.getCandidateAnswerLogCapacity() <= num){//发送任务
                log.info("-----推送大易数据条数为:{}",jobsSize.get());
                for(ReportAndSendScoreDayiDTO dto : jobs){
                    iThirdInterfaceService.finishExamCallback(dto.getExamId(),dto.getCandidateId());
                }
                jobs.clear();
                jobsSize.set(0);
            }
        } finally {
            lock.unlock();
        }
    }

    private void timeToConsumer(){
        log.info("dayi推送定时任务启动成功");
        try {
            lock.lock();
            if(jobsSize.get() > 0){
                log.info("-----定时任务推送大易数据条数为:{}",jobsSize.get());
                for(ReportAndSendScoreDayiDTO dto : jobs){
                    iThirdInterfaceService.finishExamCallback(dto.getExamId(),dto.getCandidateId());
                }
                jobs.clear();
                jobsSize.set(0);
            }
        } finally {
            lock.unlock();
        }
        log.info("dayi推送定时任务处理完成");
    }


    @Override
    public void destroy() throws Exception {
        if(jobsSize.get() > 0){
            log.info("-----推送大易数据条数为:{}",jobsSize.get());
            for(ReportAndSendScoreDayiDTO dto : jobs){
                iThirdInterfaceService.finishExamCallback(dto.getExamId(),dto.getCandidateId());
            }
        }
        jobs.clear();
        jobsSize.set(0);
    }
}
